package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.annotation.Nullable;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingDetailEntity5;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class CustomFieldViewAdapter extends BaseRecycleAdapter<EnterpriseBiddingDetailEntity5.TenderingApplysEntity> {

    private EditText et_title;
    private TextView tv_title_num;
    private ImageView iv_close;
    private EditText et_context;
    private TextView tv_context_num;

    public CustomFieldViewAdapter(Context context, List<EnterpriseBiddingDetailEntity5.TenderingApplysEntity> datas) {
        super(context, R.layout.layout_custom_field_view, datas);
        this.datas = new ArrayList<>();
    }

    @Override
    public void convert(ItemViewHolder holder, EnterpriseBiddingDetailEntity5.TenderingApplysEntity tenderingApplys) {
        et_title = holder.itemView.findViewById(R.id.et_title);
        tv_title_num = holder.itemView.findViewById(R.id.tv_title_num);
        iv_close =  holder.itemView.findViewById(R.id.iv_close);
        et_context =  holder.itemView.findViewById(R.id.et_context);
        tv_context_num =  holder.itemView.findViewById(R.id.tv_context_num);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datas.remove(tenderingApplys);
                notifyDataSetChanged();
                if (onItemClick!= null){
                    onItemClick.onItemClick(null,holder.getAdapterPosition());
                }
            }
        });
        et_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                tenderingApplys.setApplyTitle(s.toString().trim());
                tv_title_num.setText(s.length() + "/10");
                if (onItemClick != null){
                    onItemClick.onItemClick(tenderingApplys,holder.getAdapterPosition());
                }
            }
        });
        et_context.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                tenderingApplys.setApplyContent(s.toString().trim());
                tv_context_num.setText(s.length() + "/200");
                if (onItemClick != null){
                    onItemClick.onItemClick(tenderingApplys,holder.getAdapterPosition());
                }
            }
        });

        if (!TextUtils.isEmpty(tenderingApplys.getApplyTitle())){
            et_title.setText(tenderingApplys.getApplyTitle());
        }else{
            et_title.setText("");
        }
        if (!TextUtils.isEmpty(tenderingApplys.getApplyContent())){
            et_context.setText(tenderingApplys.getApplyContent());
        }else{
            et_context.setText("");
        }
        if (onItemClick != null){
            onItemClick.onItemClick(tenderingApplys,holder.getAdapterPosition());
        }
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
